class <%= class_name %>PagesController < ApplicationController
  
  before_filter :login_required, :only => [:new, :edit, :update, :create]
  before_filter :check_administrator_role, :only => [:destroy]
  
  # GET /<%= file_name %>_pages
  # GET /<%= file_name %>_pages.xml
  def index
    respond_to do |format|
      format.html                    
      format.xml  { render :xml => @<%= file_name %>_pages }
    end
  end

  def search
    if params[:<%= file_name %>_query] and params[:<%= file_name %>_query].size > 0
      @<%= file_name %>_pages = <%= class_name %>Page.find_with_ferret("*" + params[:<%= file_name %>_query].gsub(" ", "* *") + "*")
    end
    
    respond_to do |format|
      format.html                    
    end
  end
  
  def changes(limit = 10)
    # show the 10 most recently updated pages
    @<%= file_name %>_pages = <%= class_name %>Page.find(:all, :limit => limit, :order => "updated_on DESC")
    
    respond_to do |format|
      format.html # {render :action => 'search'}                   
    end
  end

  # returns all wiki pages that fit under this category
  def category
    @<%= file_name %>_pages = <%= class_name %>Page.find_tagged_with(params[:id])
    
    respond_to do |format|
      format.html                    
    end
    
  end

  # GET /<%= file_name %>_pages/1
  # GET /<%= file_name %>_pages/1.xml
  def show

    # TODO: send user to a "make me" page if the link isn't valid 
    if params[:id] == "Random"
      # NOTE: rand is very inefficient
      @<%= file_name %>_page = <%= class_name %>Page.find(:first, :order => "rand()")
    else
      @<%= file_name %>_page = <%= class_name %>Page.find_by_title(params[:id].gsub('_',' '))
    end
    respond_to do |format|
     if @<%= file_name %>_page.nil?
      format.html { render :template => "<%= file_name %>_pages/no_page.html.erb"} 
     else
      format.html { redirect_to(<%= file_name %>_path(@<%= file_name %>_page.slug)) if params[:id] == "Random"}
     end
      format.xml  { render :xml => @<%= file_name %>_page }
    end
  end

  # GET /<%= file_name %>_pages/new
  # GET /<%= file_name %>_pages/new.xml
  def new
    @<%= file_name %>_page = <%= class_name %>Page.new
    
    @<%= file_name %>_page.title = params[:title].gsub('_',' ') if params[:title]

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @<%= file_name %>_page }
    end
  end

  # GET /<%= file_name %>_pages/1/edit
  def edit
    @<%= file_name %>_page = <%= class_name %>Page.find_by_title(params[:id].gsub('_',' '))
  end

  # POST /<%= file_name %>_pages
  # POST /<%= file_name %>_pages.xml
  def create
    # TODO: do checks for proper inputs (empty body, or bad title)
    @<%= file_name %>_page = <%= class_name %>Page.new(params[:<%= file_name %>_page])

    @<%= file_name %>_page.set_tag_list_on(:categories, params[:tags]) if params[:tags]


    respond_to do |format|
      if @<%= file_name %>_page.save and @<%= file_name %>_page.revisions << WikiRevision.new(:body => params[:<%= file_name %>_body], :user_id => current_user.id, :edit_summary => "Initial Revision")
                
        #flash[:notice] = '<%= class_name %>Page was successfully created.'
        format.html { redirect_to(<%= file_name %>_path(@<%= file_name %>_page.slug)) }
        format.xml  { render :xml => @<%= file_name %>_page, :status => :created, :location => @<%= file_name %>_page }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @<%= file_name %>_page.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /<%= file_name %>_pages/1
  # PUT /<%= file_name %>_pages/1.xml
  def update
    
    @<%= file_name %>_page = <%= class_name %>Page.find_by_title(params[:id].gsub('_',' '))

    # TODO: do checks for proper input

    @<%= file_name %>_page.set_tag_list_on(:categories, params[:tags])

    respond_to do |format|
      # NOTE: Can't update the page info yet, a change in title would break links
      #if @<%= file_name %>_page.update_attributes(params[:<%= file_name %>_page])

      # make new revion for this wiki page
      # NOTE: be sure to save the revision first since it is used in the search index
      if @<%= file_name %>_page.revisions << WikiRevision.new(:user_id => current_user.id, :body => params[:<%= file_name %>_body], :edit_summary => params[:edit_summary]) and @<%= file_name %>_page.save 
        
        format.html { redirect_to(<%= file_name %>_path(@<%= file_name %>_page.slug)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @<%= file_name %>_page.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /<%= file_name %>_pages/1
  # DELETE /<%= file_name %>_pages/1.xml
  def destroy
    @<%= file_name %>_page = <%= class_name %>Page.find_by_title(params[:id].gsub('_',' '))
    @<%= file_name %>_page.destroy

    respond_to do |format|
      format.html { redirect_to(<%= file_name %>_index_path) }
      format.xml  { head :ok }
    end
  end
  
  # take the preview paramter and return preview partial 
  def preview
    
    @preview_revision = WikiRevision.new(:body => params[:preview])
    
    respond_to do |format|
      format.js { render :partial => "preview.html.erb" }
    end
  end
end
